﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Entities;
using Context;
namespace WCF_ListenMusic.Services.Module_Album.Public
{    
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "AlbumService" in code, svc and config file together.
    public class AlbumService : IAlbumService
    {
        Context.Connect_DBDataContext context = new Context.Connect_DBDataContext();
        public List<View_BXH_Album> getBXH_Album()
        {
            try {
                var q = from a in context.View_BXH_Albums
                        select a;
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }


        public List<Album> getAllAlbum()
        {
            try
            {
                var q = from a in context.Albums
                        select a;
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }



        public Album getAlbum_byID(int albumid)
        {
            try
            {
                var q = from a in context.Albums
                        where a.AlbumID.Equals(albumid)
                        select a;
                return q.Single();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }


        public List<Album> getAllAlbum_bySingerID(int singerid)
        {
            try
            {
                var q = from a in context.Albums
                        where a.SingerID.Equals(singerid)
                        select a;
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }


        public List<View8_Random_Album> get8Random_Album()
        {
            try
            {
                var q = from a in context.View8_Random_Albums                       
                        select a;
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }


        public List<AlbumSinger> getAllAlbum_byAlbumName(string albumName)
        {
            try
            {
                var q = from a in context.AlbumSingers
                        where a.AlbumName.Trim().ToLower().Contains(albumName.Trim().ToLower())
                        select a;
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }


        public List<AlbumSinger> get5New_album()
        {
            try
            {
                var q = (from a in context.AlbumSingers                        
                        orderby a.DateUp descending
                        select a).Take(5);
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }

        public int countAlbum_byCategoryid(int categoryid)
        {
            try
            {
                var q = (from a in context.AlbumSingers
                        where a.CategoryID.Equals(categoryid)
                        select a).Count();
                return q;
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }

        public List<AlbumSinger> getAlbum_byCategoryid(int categoryid)
        {
            try
            {
                var q = from a in context.AlbumSingers
                        where a.CategoryID.Equals(categoryid)
                        select a;
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }


        public List<CountryAlbum> getRangeAlbum_byCountryID(int countryid)
        {
            try
            {
                var q = (from a in context.CountryAlbums
                        where a.CountryID.Equals(countryid)
                        orderby a.CountView descending
                        select a).Take(30);
                return q.ToList();
            }
            catch (Exception ex) { throw new FaultException(ex.Message); }
        }


        public bool updateCountView(int albumid)
        {
            try
            {
                var q =  from a in context.Albums
                         where a.AlbumID.Equals(albumid)                         
                         select a;
                Album al = q.Single();
                al.CountView += 1;
                context.SubmitChanges();
                return true;
            }
            catch (Exception ex) { return false; throw new FaultException(ex.Message); }
        }




        public List<AlbumSinger> getAllAlbumSinger_bySingerID(int singerid)
        {
            try
            {
                var q = from a in context.AlbumSingers
                        where a.SingerID.Equals(singerid)
                        select a;
                return q.ToList();
            }
            catch (Exception e) { throw new FaultException(e.Message); }
        }


        public List<AlbumSinger> getAllAlbumSinger_bySingerName(string singerName)
        {
            try
            {
                var q = from a in context.AlbumSingers
                        where a.SingerName.Trim().ToLower().Contains(singerName.Trim().ToLower())
                        select a;
                return q.ToList();
            }
            catch (Exception e) { throw new FaultException(e.Message); }
        }
    }
}
